package servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.AppointmentDao;
import session.CommentDao;
import entity.Appointment;
import entity.Comment;
/**
 * Kontroler koji vrši pripremu podataka za
 * listAppointmentDetails.jsp
 * koji su potrebni za listiranje budućih termina.
 * @author rigo & jung
 *
 */
public class PrepareAppointmentView extends HttpServlet {
	private static final long serialVersionUID = -1599399647812238318L;
	@EJB
	protected AppointmentDao appointmentDao;
	@EJB
	protected CommentDao commentDao;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String appId = request.getParameter("appId");
			if (appId==null || appId.trim().equalsIgnoreCase("")) {
				response.sendRedirect(response.encodeURL("./main.jsp"));
				return;
			}
			Appointment app = appointmentDao.findById(Integer.parseInt(appId));
			List<Comment> comments = commentDao.getCommentsByAppointment(app);
			request.setAttribute("appointment", app);
			request.setAttribute("comments", comments);
			getServletContext().getRequestDispatcher("/listAppointmentDetails.jsp").forward(request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
